/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pt.ipleiria.plp.ui;

import de.enough.polish.ui.Alert;
import de.enough.polish.ui.AlertType;
import de.enough.polish.ui.Command;
import de.enough.polish.ui.Displayable;
import de.enough.polish.util.Locale;
import pt.ipleiria.plp.BlackSpotterMidlet;
import pt.ipleiria.plp.Lixeiras.Lixeira;
import pt.ipleiria.plp.Lixeiras.LixeiraEdited;
import pt.ipleiria.plp.Lixeiras.LixeiraPeddingComplete;
import pt.ipleiria.plp.Lixeiras.LixeiraSent;
import pt.ipleiria.plp.stub.BlackSpotState;
import pt.ipleiria.plp.stub.WasteVolume;

/**
 *
 * @author filipe
 */
public class EditarEventoScreen extends EventoScreen {

    private Lixeira lix;
    private boolean enviada;

    public EditarEventoScreen(BlackSpotterMidlet midlet, LixeiraPeddingComplete pend) {
        super(midlet, Locale.get("title.EditarLixeira"), pend.getImagesTitles());

        setLixTitle(pend.getTitle());

        if (pend instanceof LixeiraEdited) {
            setLixTitleBlocked();
        }

        setPos(pend.getLatitude() + "," + pend.getLongitude());
        setDescBox(pend.getNotes());
        setTam(pend.getQuantity());
        setCatLix(pend.getClassification());
        setVol(pend.getEstimatedWasteVolume());
        setEstVolunt(pend.getEstimatedNumberVolunteers());
        setToClear(pend.isDelImages());
        setTagsList(pend.getTags().getString());

        this.lix = pend;
        this.enviada = false;
        createTabs(true);


    }

    public EditarEventoScreen(BlackSpotterMidlet midlet, LixeiraSent sent) {
        super(midlet, Locale.get("title.EditarLixeira"), sent.getImagesTitles());

        setLixTitle(sent.getTitle());

        setLixTitleBlocked();

        setPos(sent.getBs().getLatitude() + "," + sent.getBs().getLongitude());
        setDescBox(sent.getBs().getNotes());
        setTam(sent.getBs().getQuantity());
        for(int i = 0; i < sent.getBs().getClassification().length;i++){
            System.out.println("Class: "+sent.getBs().getClassification()[i]);
        }
        setCatLix(sent.getBs().getClassification());
        setVol(sent.getBs().getEstimatedWasteVolume());
        setEstVolunt(sent.getBs().getEstimatedNumberVolunteers());
        setToClear(false);
        setTagsList(sent.getBs().getTags().getString());

        this.lix = sent;
        this.enviada = true;
        createTabs(true);

    }

    public void commandAction(Command c, Displayable d) {
        if (c == getCmdExit()) {
            //#style popupAlert
            Alert alert = new Alert(Locale.get("title.noSave"),
                    Locale.get("mensagens.noSaveEdit"), null, AlertType.CONFIRMATION);
            alert.addCommand(getCmdNao());
            alert.addCommand(getCmdSim());
            alert.setCommandListener(this);
            getMidlet().getDisplay().setCurrent(alert);
        } else if (c == getCmdNao()) {
            getMidlet().getDisplay().setCurrent(this);
        }
        if (c == getCmdSim()) {

            getMidlet().getDisplay().setCurrent(getMidlet().getMainMenu());


        } else if (c == getCmdProx()) {
            int valid = validateFields();
            if (valid == 0) {

                if (enviada) {
                    LixeiraEdited lixEdit = new LixeiraEdited((LixeiraSent) lix,
                            getPos()[0], getPos()[1], getTam(), getCatLix(),
                            getNumVolunteers(), getEstVolume(), getDescBox(),
                            getTagsList(), isToClear());

                    lixEdit.setImages(getFotos());

                    if (getMidlet().getApp().getManager().editLixeiraSent(lixEdit)) {
                        Alert alert = new Alert(Locale.get("title.sucesso"),
                                Locale.get("mensagens.sucesso"), null, AlertType.INFO);
                        getMidlet().getDisplay().setCurrent(alert, getMidlet().getMainMenu());
                    }
                } else {

                    LixeiraPeddingComplete lixP = (LixeiraPeddingComplete) lix;

                    lixP.edit(getLixTitle(), getPos()[0], getPos()[1], getTam(), getCatLix(),
                            getNumVolunteers(), getEstVolume(), getDescBox(),
                            getTagsList());
                    lixP.setImages(getFotos());


                    //Caso seja edited e a pessoa cancelar o del às fotos isso tem de ser confirmado
                    if (lixP instanceof LixeiraEdited) {
                        if (((LixeiraEdited) lixP).isPendingAllFotosDelete() && !isToClear()) {
                            ((LixeiraEdited) lixP).setPendingAllFotosDelete(false);
                        }
                    }
                    Alert alert = new Alert(Locale.get("title.sucesso"),
                            Locale.get("mensagens.sucesso"), null, AlertType.INFO);
                    getMidlet().getDisplay().setCurrent(alert, getMidlet().getMainMenu());

                }

            } else {

                String error = null;

                switch (valid) {
                    case 1:
                        error = Locale.get("newEv.pos");
                        break;
                    case 2:
                        error = Locale.get("newEv.desc");
                        break;
                    case 3:
                        error = Locale.get("cats.title");
                        break;
                    case 4:
                        error = Locale.get("mensagens.tags");
                        break;
                    case 5:
                        error = Locale.get("newEv.volume");
                        break;
                    case 6:
                        error = Locale.get("newEv.numero");
                        break;
                    default:
                        error = "BUG!";
                        break;
                }
                Alert alert = new Alert(Locale.get("title.camposFalta"),
                        Locale.get("mensagens.camposFalta") + error, null, AlertType.WARNING);
                getMidlet().getDisplay().setCurrent(alert, this);

            }
        } else {
            super.commandAction(c, d);
        }





    }
}
